package cn.fl.finance.controller;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.condition.SysEmpBankAccQueryCondition;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.ass.service.IAssChnBankAccService;
import cn.fl.ass.vo.AssChnBankAccVO;
import cn.fl.finance.constant.FinLoanConstant;
import cn.fl.finance.service.IFinLoanApplyService;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.service.ISysEmpBankAccService;
import cn.fl.system.service.ISysUlFilesRecService;
import cn.fl.system.vo.SysEmpBankAccVO;
import org.aspectj.weaver.ast.Var;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 借款申请，账户绑定
 * 作者:hongyanbo
 * 时间:2018/9/30
 */
@Controller
@RequestMapping("/capital/finance/loan/apply/bankAcc")
public class FinLoanApplyBankAccBindController {
    public static final Logger LOG = LoggerFactory.getLogger(FinLoanApplyBankAccBindController.class);
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;
    @Autowired
    private ISysEmpBankAccService bankAccService;
    @Autowired
    private IFinLoanApplyService finLoanApplyService;
    @Autowired
    private ISysUlFilesRecService sysUlFilesRecService;
    @Autowired
    private IAssChnBankAccService assChnBankAccService;

    /**
     * 银行卡绑定List
     * @param model
     * @param payDtlId 付款明细id
     * @param editAble 0:不可编辑 1：可编辑
     * @return
     */
    @RequestMapping("toListPage.do")
    public ModelAndView toList(ModelAndView model,@RequestParam(required = true) Long payDtlId,@RequestParam(required = false,defaultValue = "1") String editAble){
       model.setViewName("finance/loan/apply/finLoanApplyBankAccountList");
       model.addObject("payDtlId",payDtlId);
       model.addObject("editAble",editAble);
       return model;
    }

    /**
     *  初始化列表
     * @return
     */
    @RequestMapping("/initList.do")
    @ResponseBody
    public ListPageVO initList(int page, int pagesize){
        Pager pager = new Pager();
        pager.setEveryPage(pagesize);
        pager.setCurrentPage(page);
        SysEmpBankAccQueryCondition qc = new SysEmpBankAccQueryCondition();
        qc.setUsrId(CurrentThreadContext.getCurrentUserId());
        pager.setCondition(qc);
        pager = this.bankAccService.searchListPage(pager);
        List<SysEmpBankAccVO>resultList = (List<SysEmpBankAccVO>) pager.getResultList();
        ListPageVO result = new ListPageVO(null,0);
        if(CollectionUtils.isNotEmpty(resultList)){
            result = new ListPageVO(resultList,pager.getTotalCount());
        }
        return result;
    }

    /**
     * 绑定银行卡
     * @param payDtlId
     * @param bankAccId
     */
    @RequestMapping("/bind.do")
    @ResponseBody
    public DataResultVO bind(@RequestParam Long payDtlId,@RequestParam Long bankAccId){
        return finLoanApplyService.bindBankAcc(payDtlId, bankAccId);
    }

    /**
     * 新增银行账号页面
     * @param model
     * @return
     */
    @RequestMapping("/toAddPage.do")
    public ModelAndView toAddPage(ModelAndView model){
        // bank dictionary list
        List<KeyValueVO> bankDicList = sysDictionaryApiService.getKvList(FinLoanConstant.FIN_BANK_DIC, FinLoanConstant.SYS_CD, FinLoanConstant.ORG_CD);
        List<KeyValueVO> accTypDicList = sysDictionaryApiService.getKvList(FinLoanConstant.FIN_ACC_TYP_DIC, FinLoanConstant.SYS_CD, FinLoanConstant.ORG_CD);
        model.addObject("bankDicList",bankDicList);
        model.addObject("accTypDicList",accTypDicList);
        model.setViewName("finance/loan/apply/finLoanApplyBankAccountAdd");
        return model;
    }

    /**
     * 查看
     * @param bkId sys_emp_bank_acc.id  or  ass_chn_bank_acc.id
     * @return
     */
    @RequestMapping("/view.do")
    public ModelAndView view(ModelAndView model ,Long bkId){
        SysEmpBankAccVO bankAccVO = this.bankAccService.selectById(bkId);
        if(bankAccVO == null){ //查另一张表
            AssChnBankAccVO assChnBankAccVO = assChnBankAccService.selectById(bkId);
            // 转化
            bankAccVO = new SysEmpBankAccVO();
            bankAccVO.setRcptUnt(assChnBankAccVO.getAccNm());
            bankAccVO.setAccNo(assChnBankAccVO.getAccNo());
            bankAccVO.setBkNo(assChnBankAccVO.getBkNo());
            bankAccVO.setBkCd(assChnBankAccVO.getBkCd());
        }
        model.setViewName("finance/loan/apply/finLoanApplyBankAccountView");
        List<KeyValueVO> bankDicList = sysDictionaryApiService.getKvList(FinLoanConstant.FIN_BANK_DIC, FinLoanConstant.SYS_CD, FinLoanConstant.ORG_CD);
        List<KeyValueVO> accTypDicList = sysDictionaryApiService.getKvList(FinLoanConstant.FIN_ACC_TYP_DIC, FinLoanConstant.SYS_CD, FinLoanConstant.ORG_CD);
        model.addObject("bankDicList",bankDicList);
        model.addObject("accTypDicList",accTypDicList);
        model.addObject("vo",bankAccVO);

        return model;
    }

    @RequestMapping("/add.do")
    @ResponseBody
    public DataResultVO add(SysEmpBankAccVO vo,String rgnProvCd,String rgnCityCd,String uploadParam){
        DataResultVO res = new DataResultVO();
        if(StringUtils.isNotBlank(rgnProvCd)){
            vo.setRgnPrCd(rgnProvCd);
        }
        if(StringUtils.isNotBlank(rgnCityCd)){
            vo.setRgnCyCd(rgnCityCd);
        }
        vo.setUsrId(CurrentThreadContext.getCurrentUserId());
        try {
            res.setSuccess(1);
            res.setInfo("新增成功");
            res.setData(null);
            Long id = bankAccService.add(vo);
            uploadAttchment(uploadParam,res,id);
        }catch (Exception e){
            res.setSuccess(1);
            res.setInfo(e.getMessage());
            res.setData(null);
            BaseLogUtils.error(LOG,"add","新增银行卡失败："+ e.getMessage());
        }
        return res;

    }

    private void uploadAttchment(String attchmnt,  DataResultVO result, Long id) {
        if(StringUtils.isNotEmpty(attchmnt)){
            //上传解析
            try{
                String cfgCd = "FIN001";
                String cfgDtlCd = "10";
                String tabNm = "SYS_EMP_BANK_ACC";
                String cfgOrgCd = "0000";
                Boolean attchmntPass = this.sysUlFilesRecService.addAttchmnt(String.valueOf(id), attchmnt, cfgCd, cfgDtlCd, tabNm, "附件下载!", null, null, false, cfgOrgCd);
                if(null == attchmntPass || attchmntPass == false){
                    result.setInfo(result.getInfo()+"-附件上传失败!");
                }
            }catch(Exception e){
                BaseLogUtils.error(LOG, "uploadAttchment", result.getInfo()+"-附件上传失败!", e);
                result.setInfo(result.getInfo()+"-附件上传失败!");
            }
        }
    }



}
